package ru.ipo.dces.plugins.admin;

import info.clearthought.layout.TableLayout;
import ru.ipo.dces.client.Controller;
import ru.ipo.dces.client.Localization;
import ru.ipo.dces.client.Settings;
import ru.ipo.dces.client.components.ContestChoosingPanel;
import ru.ipo.dces.clientservercommunication.ContestDescription;
import ru.ipo.dces.clientservercommunication.GetContestDataResponse;
import ru.ipo.dces.clientservercommunication.UserDataField;
import ru.ipo.dces.pluginapi.Plugin;
import ru.ipo.dces.pluginapi.PluginEnvironment;
import ru.ipo.dces.plugins.admin.components.*;
import ru.ipo.dces.server.ServerFailReason;
import ru.ipo.dces.utils.ResponseHandler;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class
        LoginPluginV2 extends JPanel implements Plugin {

    private ContestChoosingPanel contestChoosingPanel = new ContestChoosingPanel();
    private ContestRegistrationPanel contestRegistrationPanel = new ContestRegistrationPanel();
    private ContestInformationPanel contestInformationPanel = new ContestInformationPanel();
    private ContestDescriptionPanel contestDescriptionPanel = new ContestDescriptionPanel();
    private LoginPluginMainLoginPanel mainLoginPanel = new LoginPluginMainLoginPanel();
    public static final int separatorGap = 5;
    private final int verticalGap = 10;
    private TableLayout pluginLayout = new TableLayout(
            new double[]{TableLayout.FILL, 275, 225, 275, TableLayout.FILL},
            new double[]{verticalGap, TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED, 200, verticalGap}
    );

    /**
     * Инициализация plugin'а
     *
     * @param env environment for the plugin
     */

    public LoginPluginV2(PluginEnvironment env) {
        env.setTitle(Localization.getAdminPluginName(LoginPluginV2.class));
        //set list model for contest list
        if (Settings.getInstance().getContestIdForInterfaceRestriction() == -1)
            setupDefaultUI();
        else
            setupContestUI();

        //show contests
        //contestChoosingPanel.refreshContestList(); //will be done by ativate() method

        if (Settings.getInstance().getContestIdForInterfaceRestriction() == -1)
            //set table models
            setNoSelfRegistration();
    }

    private void setNoSelfRegistration() {
        contestRegistrationPanel.getRegisterToContestTable().setData(new String[0], new String[0]);
        contestRegistrationPanel.getRegistrationButton().setEnabled(false);
    }

    private void setSelfRegistration(UserDataField[] data) {
        contestRegistrationPanel.getRegisterToContestTable().setKeys(data);
        contestRegistrationPanel.getRegistrationButton().setEnabled(true);
    }

    public JPanel getPanel() {
        return this;
    }

    public void activate() {
        contestChoosingPanel.refreshContestList();
    }

    public void deactivate() {
        if (Settings.getInstance().getContestIdForInterfaceRestriction() == -1) {
            contestRegistrationPanel.resetPanel();
            contestRegistrationPanel.getRegistrationButton().setEnabled(false);
            contestInformationPanel.clear();
            contestDescriptionPanel.resetPanel();
        }
    }

    private void setupDefaultUI() {
        setLayout(pluginLayout);

        add(getContestChoosingPanel(), "1, 1, 1, 2");
        add(mainLoginPanel, "2, 1");
        add(new ContestListLegend(), "2, 2");
        add(contestRegistrationPanel, "3, 1, 3, 2");
        add(new JSeparator(), "1, 3, 3, 3");
        add(contestInformationPanel, "1, 4");
        add(contestDescriptionPanel, "2, 4, 3, 4");
    }

    private void setupContestUI() {
        ResponseHandler<GetContestDataResponse> handler = new ResponseHandler<GetContestDataResponse>() {
            @Override
            public void success(GetContestDataResponse response) {
                ContestDescription defaultContest = response.contest;
                setLayout(pluginLayout);

                add(mainLoginPanel, "2, 1");
                mainLoginPanel.setContest(defaultContest);
                add(new JSeparator(), "1, 3, 3, 3");
                add(contestInformationPanel, "1, 4");
                contestInformationPanel.setContest(defaultContest);
                add(contestDescriptionPanel, "2, 4, 3, 4");
            }

            @Override
            public void fail(ServerFailReason exception) {
                System.err.println("failed to get contest info");
                System.exit(2);
            }
        };
        Controller.getContestData(Settings.getInstance().getContestIdForInterfaceRestriction(), handler);
    }

    private JPanel getContestChoosingPanel() {
        JPanel topLeftPanel = new JPanel();
        topLeftPanel.setLayout(new TableLayout(new double[]{TableLayout.FILL, TableLayout.PREFERRED},
                new double[]{TableLayout.FILL}));

        contestChoosingPanel.setShowAdminContest(Settings.getInstance().showAdminContest());
        contestChoosingPanel.addContestChangedActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ContestDescription cd = contestChoosingPanel.getContest();
                contestRegistrationPanel.resetPanel();

                if (cd == null) {
                    //clear contents of all controls
                    mainLoginPanel.resetPanel();
                    contestDescriptionPanel.resetPanel();
                    setNoSelfRegistration();
                    contestInformationPanel.clear();
                    return;
                }

                contestDescriptionPanel.setContest(cd);
                contestInformationPanel.setContest(cd);
                contestRegistrationPanel.setContest(cd);
                mainLoginPanel.setContest(cd);

                if (cd.registrationType == ContestDescription.RegistrationType.Self) {
                    setSelfRegistration(cd.data);
                } else {
                    setNoSelfRegistration();
                }
            }
        });

        topLeftPanel.add(contestChoosingPanel, "0, 0");
        return topLeftPanel;
    }
}